package com.example.win.android_banduyujia.peopleactivity.activity.activity.openactivity.courseactivity.database;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.win.android_banduyujia.database.BaseDatabaseHelper;
import com.example.win.android_banduyujia.database.DBHelper;
import com.example.win.android_banduyujia.entity.TodayEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Win on 2015/9/21.
 */
public class MyTodayDatabase extends BaseDatabaseHelper {
    public static final String TABLE_NAME_MY_TODAY="tb_my_today";

    private static MyTodayDatabase  mInstance;
    private List<TodayEntity> allToday;
    private TodayEntity todayInfo;

    protected MyTodayDatabase(String tableName) {
        super(tableName);
    }

    public static MyTodayDatabase getInstance(){
        if(mInstance == null){
            mInstance = new MyTodayDatabase(TABLE_NAME_MY_TODAY);
        }
        return mInstance;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(String.format(SQL_CREATE_PROJECT, TABLE_NAME_MY_TODAY));
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
          db.execSQL(String.format(DBHelper.SQL_TODAY_FORMAT, TABLE_NAME_MY_TODAY));
          onCreate(db);
    }

    public List<TodayEntity> getAllToday() {
        SQLiteDatabase  db = null;
        Cursor  cursor = null;
        List<TodayEntity>  list = new ArrayList<TodayEntity>();
        try{
           db = DBHelper.getInstance().getReadableDatabase();
           cursor = db.query(TABLE_NAME_MY_TODAY, null, null, null, null, null, null);

            while (cursor.moveToNext()){
                TodayEntity entity = getTodayInfo(cursor);
                list.add(entity);
            }
            return list;
        }catch (Exception e){
            return list;
        }finally {
            if(cursor != null)
                cursor.close();
        }
    }
}
